import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import java.io.Serializable;

public class Res implements Serializable {
    private static final long serialVersionUID = 1L;
    // 结果标记(true:执行成功 false:执行失败)
    private Boolean flag;
    // 消息状态码
    private Integer code;
    // 消息
    private String msg;
    // 返回数据
    private Object data;

    public Res(){}
    public Res(Boolean flag, Integer code, String msg, Object data) {
        this.flag = flag;
        this.code = code;
        this.msg = msg;
        this.data = data;
    }
    public Boolean getFlag() {
        return flag;
    }
    public void setFlag(Boolean flag) {
        this.flag = flag;
    }
    public Integer getCode() {
        return code;
    }
    public void setCode(Integer code) {
        this.code = code;
    }
    public String getMsg() {
        return msg;
    }
    public void setMsg(String msg) {
        this.msg = msg;
    }
    public Object getData() {
        return data;
    }
    public void setData(Object data) {
        this.data = data;
    }
    public <T> T getData(TypeReference<T> typeReference){
        String jsonString = JSON.toJSONString(data);
        T t = JSON.parseObject(jsonString, typeReference);
        return t;
    }


    /**
     * 1-1 响应成功
     * @return Res
     */
    public static Res ok(){
        return new Res(true,200,"响应成功",null);
    }

    /**
     * 1-2 响应成功(带返回数据)
     * @param data 返回数据
     * @return Res
     */
    public static Res ok(Object data){
        return new Res(true,200,"响应成功",data);
    }


    /**
     * 2-1 响应错误(不带状态码,带消息)
     * @return Res
     */
    public static Res error(){
        return new Res(false,401,"未知异常,请联系管理员",null);
    }

    /**
     * 2-2 响应错误(不带状态码,带消息)
     * @return Res
     */
    public static Res error(String msg){
        return new Res(false,401,msg,null);
    }

    /**
     * 2-3 响应错误(带错误码,消息提醒)
     * @return
     */
    public static Res error(Integer code, String msg){
        return new Res(false,code,msg,null);
    }


}
